﻿using System;
using System . Collections . Generic;
using System . Linq;
using System . Text;
using System . IO;
using System . Threading . Tasks;
using System . Xml . Linq;
using System . Xml;
using System . Reflection;
using WenceyWang . RichMan4L . Players;

namespace WenceyWang . RichMan4L . Players . Models
{
	public class PlayerModelProxy
	{

		public string FileName { get; set; }

		public string Name { get; set; }

		public string Introduction { get; set; }

		public Uri Image { get; set; }

		PlayerModel model = null;

		public PlayerModel Model { get { return model ?? ( model = new PlayerModel ( FileName ) ); } }
		

		public PlayerModelProxy ( XElement element )
		{
			if ( element==null )
			{
				throw new ArgumentNullException ( nameof ( element ) );
			}
			Name = element . Attribute ( nameof ( Name ) ) . Value;
			FileName = element . Attribute ( nameof ( FileName ) ) . Value;
			Image = new Uri ( element . Attribute ( nameof ( Image ) ) . Value );
		}

		static List<PlayerModelProxy> playerModels = null;

		public static List<PlayerModelProxy> GetPlayerModels ( )
		{
			if ( null == playerModels )
			{
				LoadPlayerModels ( ) . Wait ( );
			}

			return playerModels;

		}

		public static Task LoadPlayerModels ( )
		{
			Task loadTask = new Task ( ( ) =>
			{
				playerModels = new List<PlayerModelProxy> ( );

				var doc = ResourceHelper . LoadXmlDocument ( @"PlayerModelResource.Index.xml" );

				foreach ( var item in doc . Root . Elements ( ) )
				{
					playerModels . Add ( new PlayerModelProxy ( item ) );
				}

			} );

			loadTask . Start ( );

			return loadTask;

		}

	}
}


